﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;

public static class JsonHelper
{
    public static string ToJson(this object obj)
    {
        JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
        if (obj.GetType().FullName == typeof(DataTable).FullName)
        {
            DataTable dataTable = obj as DataTable;
            ArrayList arrayList = new ArrayList();
            foreach (DataRow dataRow in dataTable.Rows)
            {
                Dictionary<string, object> dictionary = new Dictionary<string, object>();
                foreach (DataColumn dataColumn in dataTable.Columns)
                {
                    dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName]);
                }
                arrayList.Add(dictionary);
            }
            return javaScriptSerializer.Serialize(arrayList);
        }
        return javaScriptSerializer.Serialize(obj);
    }

    public static string JsonSerializer<T>(T t)
    {
        DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(T));
        MemoryStream memoryStream = new MemoryStream();
        dataContractJsonSerializer.WriteObject(memoryStream, t);
        string @string = Encoding.UTF8.GetString(memoryStream.ToArray());
        memoryStream.Close();
        string pattern = "\\\\/Date\\((\\d+)\\+\\d+\\)\\\\/";
        MatchEvaluator evaluator = new MatchEvaluator(JsonHelper.ConvertJsonDateToDateString);
        Regex regex = new Regex(pattern);
        return regex.Replace(@string, evaluator);
    }

    public static T JsonDeserialize<T>(string jsonString)
    {
        string pattern = "\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}";
        MatchEvaluator evaluator = new MatchEvaluator(JsonHelper.ConvertDateStringToJsonDate);
        Regex regex = new Regex(pattern);
        jsonString = regex.Replace(jsonString, evaluator);
        DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(T));
        MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
        return (T)((object)dataContractJsonSerializer.ReadObject(stream));
    }

    private static string ConvertJsonDateToDateString(Match m)
    {
        string empty = string.Empty;
        DateTime dateTime = new DateTime(1970, 1, 1);
        dateTime = dateTime.AddMilliseconds((double)long.Parse(m.Groups[1].Value));
        dateTime = dateTime.ToLocalTime();
        return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
    }

    private static string ConvertDateStringToJsonDate(Match m)
    {
        if (m.Groups[0].Value == "0000-00-00 00:00:00") { return m.Groups[0].Value; }
        string empty = string.Empty;
        DateTime d = DateTime.Parse(m.Groups[0].Value).ToUniversalTime();
        return string.Format("\\/Date({0}+0800)\\/", (d - DateTime.Parse("1970-01-01")).TotalMilliseconds);
    }
}